<template>
    <div class="login_container">
        <div class="login_box">
            <el-form :model="loginForm" :rules="rules" ref="loginForm"  label-width="0px" class="login_form">
                <!-- 账号 -->
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user"></el-input>
                </el-form-item>
                <!-- 密码 -->
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" placeholder="请输入密码" prefix-icon="el-icon-lock"
                              type="password"
                    ></el-input>
                </el-form-item>
                <!-- 按钮 -->
                <el-form-item class="btn">
                    <el-button type="primary" @click="login('loginForm')">登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        name: 'Login',
        data() {

            return {
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                    ],
                },
                loginForm: {
                    username: '',
                    password: '',
                },
            }
        },
        mounted () {
            this.checkToken()
        },
        created() {
            this.findList()
        },
        methods: {
            async findList(){
                await this.$http.get("user/department/findList").then(function (resp) {
                    window.sessionStorage.setItem("list",JSON.stringify(resp.data.data));
                })
            },
            async checkToken () {
                if (window.localStorage.getItem('authorization') !== null) {
                    const resp = await this.$http.get("user/checkToken")
                }
            },

            login(loginForm) {
                this.$refs[loginForm].validate((valid) => {
                    if (valid){
                        const _this = this
                        if (this.loginForm.username != null && this.loginForm.password != null) {
                            this.$http.post("user/login", this.loginForm).then(function (resp) {
                                if (resp.data.code == 2000) {
                                    _this.$message.success('登录成功！')
                                    window.sessionStorage.setItem("user",_this.loginForm.username);
                                    localStorage.setItem('authorization', resp.data.data)
                                    _this.$router.push({path: "/home"});
                                } else {
                                    _this.$message.error(resp.data.msg)
                                }
                            })
                        }
                    }else {
                        this.$message.error('error submit!!')
                        return false;
                    }
                })
            }
        }
    }
</script>

<style lang="less" scoped>
    .login_container {
        background-color: #b3c0d1;
        height: 100%;
    }

    .login_box {
        width: 450px;
        height: 300px;
        background-color: #fff;
        border-radius: 5px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
    }

    .login_form {
        position: absolute;
        bottom: 20px;
        width: 100%;
        padding: 0 30px;
        box-sizing: border-box;
    }

    .btn {
        display: flex;
        justify-content: center;
    }
</style>
